<?php

namespace app\api\controller;

use app\service\OssService;
use Rtgm\sm\RtSm2;

class IndexController
{
    public function index()
    {
//        $ip= "171.223.107.65";
//        $data = getLongitudeLatitude($ip);
//        return successMessage("获取成功",[$data]);
        //$dd = new OssService();
        //return $dd->uploadOssFiles();
//        $time=time();
//        $url="http://d1.weather.com.cn/sk_2d/101270101.html?_={$time}";

        //$url="https://j.i8tq.com/weather2020/search/city.js";
        //$data = json_decode(str_replace('var city_data = ','',http_get($url)),true);

//        $file_path_url=public_path('/weather/airQuality.js');
//        $data = file_get_contents($file_path_url);
//        $data_strng = str_replace('const setAirData = ','',$data);
        //var_dump($data_strng);
        return view('index/index');
    }


    public function encode()
    {
        $sm2 = new RtSm2('hex',true);
        $generatekey = $sm2->generatekey();
        $generatePemkey = $sm2->generatePemkey();
        $public_filePath = public_path('cart/public.pem');
        $day=date("d");
        $public = $generatePemkey[1];
        if (!file_exists($public_filePath)){
            file_put_contents($public_filePath,$public);
        }else{
            $fileModifiedTime = date("d",filemtime($public_filePath));
            if ($day!=$fileModifiedTime){
                file_put_contents($public_filePath,$public);
            }
        }
        $private = $generatePemkey[0];
        $private_filePath = public_path('cart/private.pem');
        if (!file_exists($private_filePath)){
            file_put_contents($private_filePath,$private);
        }else{
            $fileModifiedTime = date("d",filemtime($private_filePath));
            if ($day!=$fileModifiedTime){
                file_put_contents($private_filePath,$private);
            }
        }
        $publicKey = $generatekey[1];
        $privateKey  = $generatekey[0];
        // sm2的非对称加解密，不建议加密太长的字符串
        $bin2hex_string = bin2hex("我是你爸爸");
        $m2EncryptData = $sm2 ->doEncrypt($bin2hex_string, $publicKey);
        $m2DecryptData = $sm2->doDecrypt($m2EncryptData,$privateKey);
        return successMessage("订单",[$m2EncryptData,$m2DecryptData]);
    }

    public function get_code()
    {
        $url = "https://j.i8tq.com/e_15/index.js?20200917";
        $data = http_get($url);
        return successMessage("",[$data]);
    }
}
